package com.hm.manage.utils;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.io.StringReader;
import java.util.Arrays;

public class XMLParserExample {
    public static void parseXMLString(String xmlString) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(xmlString));
            Document doc = builder.parse(is);
            // 现在你可以使用doc对象来访问XML结构，例如：
            NodeList returnstatus = doc.getDocumentElement().getElementsByTagName("returnstatus");
            for (int i = 0; i < returnstatus.getLength(); i++) {
                System.out.println("returnstatus: " + returnstatus.item(i).getTextContent());
            }
            //System.out.println("Root element :" + doc.getDocumentElement().getNodeName().getBytes("returnstatus")[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String xmlString = "<returnsms>" +
                "<returnstatus>Success</returnstatus>" +
                "<message>ok</message>" +
                "<remainpoint>-14381863</remainpoint>" +
                "<taskID>10483832</taskID>" +
                "<successCounts>1</successCounts>" +
                "</returnsms>";
        parseXMLString(xmlString);
    }
}
